// 商品分类全局组件的方法
import { defineStore } from 'pinia'
import { reqC1, reqC2, reqC3 } from '@/api/product/attr/index.ts'
import type { CategoryResponseData } from '@/api/product/attr/type'
import type { CategoryState } from './types/type'
const useCategoryStore = defineStore('category', {
  state: (): CategoryState => {
    return {
      c1Arr: [], //一级分类内容数组
      c1Id: '', //收集到一级分类的id
      c2Arr: [], //二级分类数组
      c2Id: '', //收集二级分类的id
      c3Arr: [], //三类分类数组
      c3Id: '', //收集三级分类的id
    }
  },
  actions: {
    //获取一级分类的方法
    async getC1() {
      const res: CategoryResponseData = await reqC1()
      if (res.code === 200) {
        this.c1Arr = res.data
      }
    },
    async getC2() {
      const res: CategoryResponseData = await reqC2(this.c1Id)
      if (res.code === 200) {
        this.c2Arr = res.data
      }
      // console.log(res)
    },
    async getC3() {
      const res: CategoryResponseData = await reqC3(this.c2Id)
      if (res.code === 200) {
        this.c3Arr = res.data
      }
    },
  },
  getters: {},
})

export default useCategoryStore
